var EquipmentFormModel = function (equipment) {
  BaseModel.call(this);
  var self = this;

  var numberFields = ['purchasePrice', 'dailyWorkingHours', 'timeoutBreakElectricityTime', 'timeoutBreakFlowTime'];
  var timeFields = ['purchaseTime', 'depreciableLife'];

  var fn = {
    initData: function (equipment) {
      if (equipment === undefined || equipment === null) {
        this.initEquipment();
        return;
      }
      this.setEquipment(equipment);
    },

    initEquipment: function () {
      var fields = ['equipmentNo', 'equipmentName', 'manufacturer', 'energyId', 'flowId', 'machining', 'equipmentImage', 'internetThingsCard',
        'upperComputer', 'deviceType'
      ];
      self.initObject(fields);

      self.purchaseTime = null;
      self.depreciableLife = null;

      self.purchasePrice = undefined;
      self.timeoutBreakFlowTime = undefined;
      self.timeoutBreakElectricityTime = undefined;
      self.dailyWorkingHours = undefined;
      self.mainSwitch = 'false';
      self.isHandOperation = 0;
      self.showInBoard = 0;
      self.sendMessage = 1;
      self.depWork = [];
      self.startProportion = 1;
      self.endProportion = 1;

      self.workTimeList = [];
      self.procedureList = [];
    },

    setEquipment: function (equipment) {
      var fields = ['id', 'equipmentNo', 'equipmentName', 'manufacturer', 'energyId', 'flowId', 'machining', 'equipmentImage',
        'mainSwitch', 'isHandOperation', 'sendMessage', 'showInBoard', 'internetThingsCard', 'upperComputer', 'deviceType'
      ];
      self.setDataFromObj(equipment, fields);
      self.setDataFromObj(equipment, ['startProportion', 'endProportion'], 1);

      var depWork = equipment.depWork;
      if (depWork !== null) {
        depWork = [depWork.deptId, depWork.workshopId];
      } else {
        depWork = '';
      }
      self.depWork = depWork;

      for (var i = 0; i < timeFields.length; i++) {
        var timeField = timeFields[i];
        var date = SysUtil.formatString2Date(equipment[timeField]);
        if (date === '') {
          date = null;
        }
        self[timeField] = date;
      }

      for (var i = 0; i < numberFields.length; i++) {
        var field = numberFields[i];
        var value = equipment[field];
        if (SysUtil.isEmpty(value)) {
          value = undefined;
        }
        self[field] = value;
      }

      var workTimeList = equipment.workTemporalIntervals;
      var toDayDate = moment().format('YYYY-MM-DD');
      workTimeList = _.map(workTimeList, function (workTime) {
        var startTime = workTime.startTime;
        startTime = moment(`${toDayDate} ${moment(startTime).format('HH:mm')}`).valueOf();
        var endTime = workTime.endTime;
        endTime = moment(`${toDayDate} ${moment(endTime).format('HH:mm')}`).valueOf();
        return {
          startTime: startTime,
          endTime: endTime
        };
      });
      workTimeList = _.orderBy(workTimeList, ['startTime'], ['asc']);
      self.workTimeList = workTimeList;

      var procedureList = equipment.machineRelationProcesses;
      procedureList = _.map(procedureList, function (procedure) {
        return {
          maxProcessTime: procedure.processMaxWorkingTime,
          processId: procedure.processIdentify + ""
        };
      });
      self.procedureList = procedureList;
    }
  };

  this.getObject = function () {
    var postData = {};
    var id = this.id;
    if (!SysUtil.isEmpty(id)) {
      postData.id = id;
    }

    var fields = ['equipmentNo', 'equipmentName', 'equipmentImage', 'manufacturer', 'energyId', 'flowId', 'machining', 'mainSwitch',
      'isHandOperation', 'showInBoard', 'sendMessage', 'upperComputer', 'deviceType', 'internetThingsCard', 'startProportion', 'endProportion'
    ];
    this.setPostData(postData, fields);

    for (var i = 0; i < numberFields.length; i++) {
      var numberField = numberFields[i];
      var value = this[numberField];
      if (value === undefined) {
        value = 0;
      }
      postData[numberField] = value;
    }

    for (var i = 0; i < timeFields.length; i++) {
      var timeField = timeFields[i];
      var value = this[timeField];
      if (!SysUtil.isEmpty(value)) {
        value = moment(value).format('YYYY-MM-DD');
        postData[timeField] = value;
      }
    }

    var depWork = this.depWork;
    if (depWork !== '') {
      postData.department = depWork[0];
      var workShop = depWork[1];
      if (!SysUtil.isEmpty(workShop)) {
        postData.workShop = workShop;
      } else {
        postData.workShop = '';
      }
    } else {
      postData.department = '';
      postData.workShop = '';
    }

    var workTimeList = this.workTimeList;
    workTimeList = _.map(workTimeList, function (workTime) {
      var startTime = workTime.startTime;
      startTime = `2000-02-01 ${moment(startTime).format('HH:mm')}`;
      var endTime = workTime.endTime;
      endTime = `2000-02-01 ${moment(endTime).format('HH:mm')}`;
      return {
        startTime: startTime,
        endTime: endTime
      };
    });
    postData.workTimeIntervals = JSON.stringify(workTimeList);

    var procedureList = this.procedureList;
    procedureList = _.map(procedureList, function (procedure) {
      var processId = procedure.processId;
      processId = Number(processId);
      var maxProcessTime = procedure.maxProcessTime;
      if (SysUtil.isEmpty(maxProcessTime)) {
        maxProcessTime = 0;
      }
      return {
        process: processId,
        maxProcessTime: maxProcessTime
      };
    });
    postData.processBeans = JSON.stringify(procedureList);
    return postData;
  };

  fn.initData(equipment);
};
